home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / pluck.cq / PLUCK.C
Text File  |  1984-10-30  |  3KB  |  88 lines

  1. #include          <stdio.h>  /* standard input output macros          */
  2. #include          <ctype.h>  /* standard c macros                     */
  3.  
  4. #define  NEWLINE  '\n'       /* crlf macro                            */
  5.  
  6. main(argc, argv)
  7. int argc;
  8. char *argv[];
  9. {
  10.      FILE *in, *out, *fopen();
  11.      int c;                  /* input character                       */
  12.      int start = 1;          /* starting column to save               */
  13.      int end   = 80;         /* last column to save                   */
  14.      int col;                /* current column                        */
  15.  
  16.      out = stdout;           /* set default output file               */
  17.      if (argc == 1)  {
  18.         printf("\nusage: pluck infile.ext <-o output.ext> <-s col> <-e col>\n\n");
  19.         printf("       -o output.exe -- default to console\n");
  20.         printf("       -s col        -- starting column default is 1\n");
  21.         printf("       -e col        -- ending column default is 256\n");
  22.         exit(0);
  23.      }
  24.      else                                          /* open input file */
  25.         if ((in = fopen(*(++argv), "r")) == NULL) {
  26.            printf("cannot read %s\n",*(argv));
  27.            exit(1);
  28.         }
  29.      argc -= 2;
  30.      argc >>= 1;
  31.                             /* parse command line for valid arguments */
  32.      while (argc-- > 0 && (*++argv)[0] == '-') {
  33.         c = tolower((*argv)[1]);
  34.         switch (c) {
  35.                      case 'o':  /* output file specified              */
  36.                                if ((out = fopen(*(++argv), "w")) == NULL) {
  37.                                   printf("cannot open %s for output",*(argv));
  38.                                   exit(1);
  39.                                }
  40.                                break;
  41.                      case 's':  /* starting column specified          */
  42.                                start = ctoi(*(++argv));
  43.                                printf("start = %d\n",start);
  44.                                if (start > end || start < 0) {
  45.                                   printf("invalid starting column\n");
  46.                                   exit(1);
  47.                                }
  48.                                break;
  49.                      case 'e':  /* ending column specified            */
  50.                                end = ctoi(*(++argv));
  51.                                printf("end = %d\n",end);
  52.                                if (end > 256 || end <= start) {
  53.                                   printf("invalid ending column\n");
  54.                                   exit(1);
  55.                                }
  56.                                break;
  57.                      default :  /* someone no read instructions       */
  58.                                printf("invalid option chosen %s\n",*argv);
  59.                                exit(1);
  60.                                break;
  61.         }
  62.      }
  63.  
  64.      col = 1;
  65.      while ((c = getc(in)) != EOF) {
  66.         if (c == NEWLINE) {
  67.            col = 0;
  68.            putc(NEWLINE, out);
  69.         }
  70.         else if (col >= start && col <= end)
  71.            putc(c, out);
  72.         col++;
  73.      }
  74. }
  75.  
  76. ctoi(in)  /* character string to integer  */
  77. char in[];
  78. {
  79.      int accum = 0;
  80.      int i;
  81.  
  82.      while(*in) {
  83.         accum *= 10;
  84.         accum += (*in++ - '0');
  85.      }
  86.      return(accum);
  87. }
  88.